
#ifndef NETBASE_TCP_SESSION_H
#define NETBASE_TCP_SESSION_H

// sys
#include <functional>
#include <stdint.h>
#include <memory>
// mine
#include "util.h"

struct evbuffer;
struct bufferevent;

namespace net_base {
/*!
 * @brief
 *  tcp communicate session, establish by Listener or Connector
 * @note
 *  IMPORTANT:
 *  if you bind TcpSessionPtr which is a type of shared_ptr to RecvCB or
 *  ErrorCB, these cb must be set to nullptr when you're going to release this
 *  TcpSession, or the TcpSession will hold a ref to itself and cause it never
 *  to be released.
 */
class TcpSession {
 public:
  typedef std::function<void (const char* data, size_t size)> RecvCB;
  typedef std::function<void (int sock_ec)> ErrorCB;

 public:
  TcpSession(bufferevent* bev);
  ~TcpSession(void);

  /*!
   * set callbacks
   */
  void SetCB(const RecvCB& recv_cb, const ErrorCB& error_cb);

  /*!
   * send raw data buffer
   */
  void Send(const char* data, size_t size);

  /*!
   * send with evbuffer
   */
  void Send(evbuffer* evbuf);

  /*!
   * get peer connection info: ip:port
   */
  bool PeerInfo(char* buf, size_t size);

 private:
  NONCOPYABLE(TcpSession);
  IMPL_DECL;
};

typedef std::shared_ptr<TcpSession> TcpSessionPtr;
} // namespace net_base

#endif // NETBASE_TCP_SESSION_H

